#!/bin/bash


Install_Nginx()
{
    echo -e "   \e[0;31m[+] Installing ${Nginx_Ver}...\e[0m"
    cd ${cur_dir}/src
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/openssl/${Openssl_Ver}.tar.gz
    [[ -d "${Openssl_Ver}" ]] && rm -rf ${Openssl_Ver}
    if [ -s ${Openssl_Ver}.tar.gz ]; then
        tar zxf ${Openssl_Ver}.tar.gz
        ./config -fPIC --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
        make depend
        make -j `grep 'processor' /proc/cpuinfo | wc -l`
        if [ $? -ne 0 ]; then
            make
        fi
        make install
        Nginx_With_Openssl="--with-openssl=${cur_dir}/src/${Openssl_Ver}"
    fi

if [ "${Memc}" = "memc" ]; then
    cd ${cur_dir}/src
    rm -fr memc-nginx-module
    rm -fr srcache-nginx-module
    git clone -b master https://github.com/openresty/memc-nginx-module.git
    git clone -b master https://github.com/openresty/srcache-nginx-module.git
    Nginx_Modules_Memc="--add-module=../memc-nginx-module --add-module=../srcache-nginx-module"
    if [ ! -d srcache-nginx-module ]; then
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://github.com/openresty/srcache-nginx-module/archive/master.zip -O srcache-nginx-module-master.zip
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://github.com/openresty/memc-nginx-module/archive/master.zip -O memc-nginx-module-master.zip
        unzip -o memc-nginx-module-master.zip
        unzip -o srcache-nginx-module-master.zip
        Nginx_Modules_Memc="--add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master"
    fi
fi
groupadd www
useradd -s /sbin/nologin -g www www
wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/soft/nginx/${Nginx_Ver}.tar.gz
tar zxf ${Nginx_Ver}.tar.gz
cd ${Nginx_Ver}
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${Nginx_With_Openssl} ${NginxMAOpt} ${Nginx_Modules_Memc}
make && make install
ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
rm -f /usr/local/nginx/conf/nginx.conf
cd ${cur_dir}
\cp conf/nginx.conf /usr/local/nginx/conf/nginx.conf
\cp conf/pathinfo.conf /usr/local/nginx/conf/pathinfo.conf
mkdir -p /usr/local/nginx/conf/vhost
vhostconf="/usr/local/nginx/conf/vhost/moodle.conf"
echo 'server' >${vhostconf}
echo '    {' >>${vhostconf}
echo '        listen 80;' >>${vhostconf}
echo '        #listen [::]:80;' >>${vhostconf}
echo "        server_name _;" >>${vhostconf}
echo '        index index.html index.htm index.php default.html default.htm default.php;' >>${vhostconf}
echo "        root  ${Default_Moodle_Dir}/moodle;" >>${vhostconf}
echo '#memc        location /memc {' >>${vhostconf}
echo '#memc            internal;' >>${vhostconf}
echo '#memc            memc_connect_timeout 100ms;' >>${vhostconf}
echo '#memc            memc_send_timeout 100ms;' >>${vhostconf}
echo '#memc            memc_read_timeout 100ms;' >>${vhostconf}
echo '#memc            set $memc_key $query_string;' >>${vhostconf}
echo '#memc            set $memc_exptime 300;' >>${vhostconf}
echo '#memc            memc_pass memcache;' >>${vhostconf}
echo '#memc        }' >>${vhostconf}
echo '        if (!-e $request_filename) {' >>${vhostconf}
echo '           rewrite "^(.*\.php)(/)(.*)$" $1?file=/$3 last;' >>${vhostconf}
echo '        }' >>${vhostconf}
echo '        location ~ [^/]\.php(/|$)' >>${vhostconf}
echo '        {' >>${vhostconf}
echo '#memc            set $key $uri$args;' >>${vhostconf}
echo '#memc            srcache_fetch GET /memc $key;' >>${vhostconf}
echo '#memc            srcache_store PUT /memc $key;' >>${vhostconf}
echo '            fastcgi_pass  unix:/run/php-cgi.sock;' >>${vhostconf}
echo '            fastcgi_index index.php;' >>${vhostconf}
echo '            include fastcgi.conf;' >>${vhostconf}
echo '            include pathinfo.conf;' >>${vhostconf}
echo '        }' >>${vhostconf}
echo '        access_log off;' >>${vhostconf}
echo '    }' >>${vhostconf}
if /usr/local/nginx/sbin/nginx -V 2>&1 | grep -Eqi 'memc-nginx' ; then
    sed -i 's:^#memc::g' ${vhostconf}
    sed -i 's:^#memc::g' /usr/local/nginx/conf/nginx.conf
fi
mkdir -p ${Default_Moodle_Dir}/moodle
chown -R www:www ${Default_Moodle_Dir}/moodle
\cp init.d/init.d.nginx /etc/init.d/nginx
\cp init.d/init.d.moodle /usr/bin/moodle
chmod +x /etc/init.d/nginx
chmod +x /usr/bin/moodle
if [ "$PM" = "yum" ]; then
    chkconfig --add php-fpm
    chkconfig --add nginx
    chkconfig php-fpm on
    chkconfig nginx on
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
 elif [ "$PM" = "apt" ]; then
    update-rc.d -f php-fpm defaults
    update-rc.d -f nginx defaults
fi
    /etc/init.d/php-fpm start
    /etc/init.d/nginx start
    /etc/init.d/mariadb restart
    rm -fr ${cur_dir}/src/${Nginx_Ver}
    rm -fr ${cur_dir}/src/memc-nginx-module*
    rm -fr ${cur_dir}/src/srcache-nginx-module*
    rm -fr ${cur_dir}/src/${Openssl_Ver}
}
